Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit433c62a

Browse files
committed
Updated Lowest Common Ancestor of a Binary Tree.java
1 parent2a5879f commit433c62a

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

‎Medium/Lowest Common Ancestor of a Binary Tree.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,21 @@
88
* }
99
*/
1010
classSolution {
11-
publicTreeNodelowestCommonAncestor(TreeNoderoot,TreeNodep,TreeNodeq) {
12-
if (root ==null ||root ==p ||root ==q) {
13-
returnroot;
11+
publicTreeNodelowestCommonAncestor(TreeNoderoot,TreeNodep,TreeNodeq) {
12+
if (root ==null) {
13+
returnnull;
14+
}
15+
if (root ==p ||root ==q) {
16+
returnroot;
17+
}
18+
TreeNodeleft =lowestCommonAncestor(root.left,p,q);
19+
TreeNoderight =lowestCommonAncestor(root.right,p,q);
20+
if (left !=null &&right !=null) {
21+
returnroot;
22+
}
23+
if (left !=null ||right !=null) {
24+
returnleft !=null ?left :right;
25+
}
26+
returnnull;
1427
}
15-
TreeNodeleftRoot =lowestCommonAncestor(root.left,p,q);
16-
TreeNoderightRoot =lowestCommonAncestor(root.right,p,q);
17-
if (leftRoot !=null &&rightRoot !=null) {
18-
returnroot;
19-
}
20-
returnleftRoot !=null ?leftRoot :rightRoot;
21-
}
2228
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp