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

Commitef94e1b

Browse files
add a solution for 1644
1 parent3fa425a commitef94e1b

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,26 @@ private TreeNode dfs(TreeNode root, TreeNode p, TreeNode q) {
4646
returnleft !=null ?left :right;
4747
}
4848
}
49+
50+
publicstaticclassSolution2 {
51+
intfound =0;
52+
53+
publicTreeNodelowestCommonAncestor(TreeNoderoot,TreeNodep,TreeNodeq) {
54+
TreeNodelca =lca(root,p,q);
55+
returnfound ==2 ?lca :null;
56+
}
57+
58+
privateTreeNodelca(TreeNoderoot,TreeNodep,TreeNodeq) {
59+
if (root ==null) {
60+
returnroot;
61+
}
62+
TreeNodeleft =lca(root.left,p,q);
63+
TreeNoderight =lca(root.right,p,q);
64+
if (root ==p ||root ==q) {
65+
found++;
66+
returnroot;
67+
}
68+
returnleft ==null ?right :right ==null ?left :root;
69+
}
70+
}
4971
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp