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

Appearance settings

Commit dcf7d1f

Browse filesBrowse files
committed
Trees
1 parent c1524b5 commit dcf7d1f
Copy full SHA for dcf7d1f

File tree

1 file changed

+105
-0
lines changed
Filter options

1 file changed

+105
-0
lines changed

‎src/leetcode2018/Trees.java

Copy file name to clipboard
+105Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package leetcode2018;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Trees {
7+
8+
public static void main(String[] args){
9+
Trees test = new Trees();
10+
TreeNode t = new TreeNode(5);
11+
t.left = new TreeNode(3);
12+
t.left.left = new TreeNode(2);
13+
t.left.right = new TreeNode(4);
14+
t.right = new TreeNode(7);
15+
//t.right.left = new TreeNode(6);
16+
//t.right.right= new TreeNode(8);
17+
test.inOrderTraversal(t);
18+
System.out.println();
19+
test.preOrderTraversal(t);
20+
System.out.println();
21+
test.postOrderTraversal(t);
22+
//System.out.println(test.minDepth(t));
23+
//test.binaryTreePaths(t);
24+
//test.lowestCommonAncestor(t, t.left.left, t.left.right);
25+
}
26+
27+
public int minDepth(TreeNode root) {
28+
if(root==null) return 0;
29+
int l = minDepth(root.left);
30+
int r = minDepth(root.right);
31+
System.out.println("l"+l+ " :"+r);
32+
if(r==0) return l+1;
33+
if(l==0) return r+1; System.out.println("AAl"+l+ " :"+r);
34+
int re= Math.min(l, r)+1; System.out.println(re);
35+
return re;
36+
37+
}
38+
39+
public int maxDepth(TreeNode root) {
40+
if(root == null) return 0;
41+
int l = maxDepth(root.left)+1;
42+
int r= maxDepth(root.right)+1;
43+
return Math.max(l,r);
44+
}
45+
46+
public void inOrderTraversal(TreeNode t){
47+
if(t!=null){
48+
inOrderTraversal(t.left);
49+
System.out.print(t.val);
50+
inOrderTraversal(t.right);
51+
}
52+
}
53+
54+
public List<String> binaryTreePaths(TreeNode root) {
55+
List<String> list = new ArrayList<>();
56+
if(root==null) return list;
57+
dfs(root,list, "");
58+
return list;
59+
}
60+
public void dfs(TreeNode root, List<String> list, String path){
61+
if(root.left==null && root.right == null){
62+
list.add(path+ root.val);
63+
return;
64+
}
65+
path = path+ root.val +"->";
66+
if(root.left!=null) dfs(root.left, list, path);
67+
if(root.right!=null) dfs(root.right, list, path);
68+
}
69+
70+
public void preOrderTraversal(TreeNode t){
71+
if(t!=null){
72+
System.out.print(t.val);
73+
preOrderTraversal(t.left);
74+
preOrderTraversal(t.right);
75+
}
76+
}
77+
78+
public void postOrderTraversal(TreeNode t){
79+
if(t!=null){
80+
postOrderTraversal(t.left);
81+
postOrderTraversal(t.right);
82+
System.out.print(t.val);
83+
}
84+
}
85+
86+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
87+
if(root==null || root==p || q== root) return root; System.out.println(root.val);
88+
TreeNode left = lowestCommonAncestor(root.left, p, q);
89+
TreeNode right = lowestCommonAncestor(root.right, p, q);
90+
if(left!= null && right!=null) { System.out.println("mid:"+root.val); return root;};
91+
System.out.println("left" +left+ " right:"+right);
92+
TreeNode re = left!=null? left: right;
93+
return re;
94+
}
95+
96+
}
97+
98+
class TreeNode{
99+
int val;
100+
TreeNode left;
101+
TreeNode right;
102+
TreeNode(int x){
103+
val=x;
104+
}
105+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.