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

Commit 40c3eb1

Browse files
committed
Leaves of binary tree: done
1 parent 5e7a814 commit 40c3eb1

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package com.leetcode.trees;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import static org.junit.jupiter.api.Assertions.assertEquals;
7+
8+
/**
9+
* Level: Medium
10+
* Problem Link: https://leetcode.com/problems/find-leaves-of-binary-tree/
11+
* Problem Description:
12+
* Given a binary tree, collect a tree's nodes as if you were doing this: Collect and remove all leaves, repeat
13+
* until the tree is empty.
14+
*
15+
* Example:
16+
* Input: [1,2,3,4,5]
17+
*
18+
* 1
19+
* / \
20+
* 2 3
21+
* / \
22+
* 4 5
23+
*
24+
* Output: [[4,5,3],[2],[1]]
25+
*
26+
* Explanation:
27+
* 1. Removing the leaves [4,5,3] would result in this tree:
28+
* 1
29+
* /
30+
* 2
31+
*
32+
* 2. Now removing the leaf [2] would result in this tree:
33+
* 1
34+
*
35+
* 3. Now removing the leaf [1] would result in the empty tree:
36+
* []
37+
*
38+
* @author rampatra
39+
* @since 2019年08月01日
40+
*/
41+
public class LeavesOfBinaryTree {
42+
43+
/**
44+
* THe idea is to perform a DFS and backtrack. While backtracking, check the height of the node and insert
45+
* the node into the list indexed by their heights.
46+
* Time Complexity:
47+
* Space Complexity:
48+
* Runtime: <a href=""></a>.
49+
*
50+
* @param root
51+
* @return
52+
*/
53+
public static List<List<TreeNode>> findLeavesOfBinaryTree(TreeNode root) {
54+
List<List<TreeNode>> levels = new ArrayList<>();
55+
findLeavesOfBinaryTree(root, levels);
56+
return levels;
57+
}
58+
59+
private static int findLeavesOfBinaryTree(TreeNode root, List<List<TreeNode>> levels) {
60+
if (root == null) return -1;
61+
62+
int leftHeight = findLeavesOfBinaryTree(root.left, levels);
63+
int rightHeight = findLeavesOfBinaryTree(root.right, levels);
64+
int height = Math.max(leftHeight, rightHeight) + 1;
65+
66+
if (height >= levels.size()) {
67+
levels.add(height, new ArrayList<>());
68+
}
69+
levels.get(height).add(root);
70+
71+
return height;
72+
}
73+
74+
public static void main(String[] args) {
75+
/*
76+
BST looks like:
77+
78+
4
79+
/ \
80+
1 7
81+
/ \ \
82+
3 8 20
83+
/ \
84+
2 6
85+
*/
86+
TreeNode root = new TreeNode(4);
87+
root.left = new TreeNode(1);
88+
root.right = new TreeNode(7);
89+
root.left.left = new TreeNode(3);
90+
root.left.right = new TreeNode(8);
91+
root.left.left.left = new TreeNode(2);
92+
root.left.left.right = new TreeNode(6);
93+
root.right.right = new TreeNode(20);
94+
95+
assertEquals("[[2, 6, 8, 20], [3, 7], [1], [4]]", findLeavesOfBinaryTree(root).toString());
96+
}
97+
}

‎src/main/java/com/leetcode/trees/TreeNode.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,9 @@ public class TreeNode {
1313
public TreeNode(int val) {
1414
this.val = val;
1515
}
16+
17+
@Override
18+
public String toString() {
19+
return val + "";
20+
}
1621
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /