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

Commit91c7533

Browse files
committed
Modified 2 solutions
1 parent8eaa969 commit91c7533

File tree

2 files changed

+53
-62
lines changed

2 files changed

+53
-62
lines changed

‎Easy/Binary Tree Paths.java

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,28 @@
88
* }
99
*/
1010
classSolution {
11-
publicList<String>binaryTreePaths(TreeNoderoot) {
12-
List<String>list =newArrayList<>();
13-
if (root !=null) {
14-
getString(root,"",list);
15-
}
16-
17-
returnlist;
11+
List<String>list;
12+
publicList<String>binaryTreePaths(TreeNoderoot) {
13+
list =newArrayList<>();
14+
if (root ==null) {
15+
returnlist;
1816
}
19-
20-
publicvoidgetString(TreeNoderoot,Strings,List<String>list) {
21-
if (root.left ==null &&root.right ==null) {
22-
list.add(s +root.val);
23-
}
24-
25-
if (root.left !=null) {
26-
getString(root.left,s +root.val +"->",list);
27-
}
28-
29-
if (root.right !=null) {
30-
getString(root.right,s +root.val +"->",list);
31-
}
17+
helper(root,newStringBuilder());
18+
returnlist;
19+
}
20+
21+
privatevoidhelper(TreeNoderoot,StringBuildersb) {
22+
if (root ==null) {
23+
return;
3224
}
25+
if (root.left ==null &&root.right ==null) {
26+
sb.append(root.val);
27+
list.add(sb.toString());
28+
}
29+
else {
30+
sb.append(root.val).append("->");
31+
helper(root.left,newStringBuilder(sb.toString()));
32+
helper(root.right,newStringBuilder(sb.toString()));
33+
}
34+
}
3335
}

‎Easy/Cousins in Binary Tree.java

Lines changed: 31 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,49 +8,38 @@
88
* }
99
*/
1010
classSolution {
11-
publicbooleanisCousins(TreeNoderoot,intx,inty) {
12-
if (root ==null) {
13-
returnfalse;
14-
}
15-
16-
DetailedNodenodeX =helper(root,null,x,0);
17-
DetailedNodenodeY =helper(root,null,y,0);
18-
19-
if (nodeX ==null ||nodeY ==null) {
20-
returnfalse;
21-
}
22-
23-
returnnodeX.depth ==nodeY.depth &&nodeX.parent !=nodeY.parent;
11+
publicbooleanisCousins(TreeNoderoot,intx,inty) {
12+
if (root ==null ||x ==y) {
13+
returntrue;
2414
}
25-
26-
privateDetailedNodehelper(TreeNoderoot,TreeNodeparent,intnum,intdepth) {
27-
if (root ==null) {
28-
returnnull;
29-
}
30-
31-
if (root.val ==num) {
32-
returnnewDetailedNode(root,parent,depth -1);
33-
}
34-
35-
DetailedNodeleft =helper(root.left,root,num,depth +1);
36-
DetailedNoderight =helper(root.right,root,num,depth +1);
37-
38-
if (left ==null) {
39-
returnright;
40-
}
41-
42-
returnleft;
15+
ParentDatap1 =newParentData();
16+
ParentDatap2 =newParentData();
17+
helper(root,x,0,null,p1);
18+
helper(root,y,0,null,p2);
19+
returnp1.depth ==p2.depth &&p1.parent !=p2.parent;
20+
}
21+
22+
privatevoidhelper(TreeNoderoot,intx,intcurrDepth,TreeNodeparent,ParentDatap) {
23+
if (root ==null) {
24+
return;
4325
}
44-
45-
classDetailedNode {
46-
publicTreeNodenode;
47-
publicTreeNodeparent;
48-
publicintdepth;
49-
50-
publicDetailedNode(TreeNodenode,TreeNodeparent,intdepth) {
51-
this.node =node;
52-
this.parent =parent;
53-
this.depth =depth;
54-
}
26+
if (root.val ==x) {
27+
p.depth =currDepth;
28+
p.parent =parent;
5529
}
30+
else {
31+
helper(root.left,x,currDepth +1,root,p);
32+
helper(root.right,x,currDepth +1,root,p);
33+
}
34+
}
35+
}
36+
37+
classParentData {
38+
intdepth;
39+
TreeNodeparent;
40+
41+
publicParentData() {
42+
this.depth =0;
43+
this.parent =null;
44+
}
5645
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp