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

Commit104c54a

Browse files
refactor 236
1 parenta08a9fd commit104c54a

File tree

1 file changed

+20
-16
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+20
-16
lines changed

‎src/main/java/com/fishercoder/solutions/_236.java

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,27 @@ For example, the lowest common ancestor (LCA) of nodes 5 and 1 is 3.
2222
Another example is LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.*/
2323

2424
publicclass_236 {
25-
26-
/**We need to find TWO nodes in the tree,
27-
* so we'll have to divide and conquer this tree,
28-
* we need to have two nodes to as the intermediate result,
29-
* also, refer to my earlier drawings:http://www.fishercoder.com/2016/06/23/lowest-common-ancestor-of-a-binary-tree/
30-
* I'm really impressed with myself at that time!*/
31-
32-
publicTreeNodelowestCommonAncestor(TreeNoderoot,TreeNodep,TreeNodeq) {
33-
if (root ==null ||root ==p ||root ==q) {
34-
returnroot;
35-
}
36-
TreeNodeleft =lowestCommonAncestor(root.left,p,q);
37-
TreeNoderight =lowestCommonAncestor(root.right,p,q);
38-
if (left !=null &&right !=null) {
39-
returnroot;
25+
publicstaticclassSolution1 {
26+
27+
/**
28+
* We need to find TWO nodes in the tree,
29+
* so we'll have to divide and conquer this tree,
30+
* we need to have two nodes to as the intermediate result,
31+
* also, refer to my earlier drawings:http://www.fishercoder.com/2016/06/23/lowest-common-ancestor-of-a-binary-tree/
32+
* I'm really impressed with myself at that time!
33+
*/
34+
35+
publicTreeNodelowestCommonAncestor(TreeNoderoot,TreeNodep,TreeNodeq) {
36+
if (root ==null ||root ==p ||root ==q) {
37+
returnroot;
38+
}
39+
TreeNodeleft =lowestCommonAncestor(root.left,p,q);
40+
TreeNoderight =lowestCommonAncestor(root.right,p,q);
41+
if (left !=null &&right !=null) {
42+
returnroot;
43+
}
44+
returnleft !=null ?left :right;
4045
}
41-
returnleft !=null ?left :right;
4246
}
4347

4448
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp