LeetCode Find Leaves of Binary Tree

原题链接在这里:https://leetcode.com/problems/find-leaves-of-binary-tree/#/description

题目:

Given a binary tree, collect a tree‘s nodes as if you were doing this: Collect and remove all leaves, repeat until the tree is empty.

Example:
Given binary tree

          1
         /         2   3
       / \
      4   5    

Returns [4, 5, 3], [2], [1].

Explanation:

1. Removing the leaves [4, 5, 3] would result in this tree:

          1
         /
        2          

2. Now removing the leaf [2] would result in this tree:

          1          

3. Now removing the leaf [1] would result in the empty tree:

          []         

Returns [4, 5, 3], [2], [1].

题解:

计算当前node到leaf的距离为当前node的高度,相同高度的点放在同一个list里.

Time Complexity: O(n). leaf层用时n/2, leaf上一层用时n/4*2, 再上一层用时n/8*3. 是n(i/2^i)的和. i = 1,2,3....

Space: O(logn), stack space. Regardless res.

AC Java:

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 public class Solution {
11     public List<List<Integer>> findLeaves(TreeNode root) {
12         List<List<Integer>> res = new ArrayList<List<Integer>>();
13         height(root, res);
14         return res;
15     }
16
17     private int height(TreeNode root, List<List<Integer>> res){
18         if(root == null){
19             return -1;
20         }
21         int h = 1+Math.max(height(root.left, res), height(root.right, res));
22         if(res.size() < h+1){
23             res.add(new ArrayList<Integer>());
24         }
25         res.get(h).add(root.val);
26         return h;
27     }
28 }

类似Maximum Depth of Binary Tree.

时间: 2024-11-05 11:32:25

LeetCode Find Leaves of Binary Tree的相关文章

[LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点

Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps until the tree is empty. Example: Given binary tree 1 / 2 3 / \ 4 5 Returns [4, 5, 3], [2], [1]. Explanation: 1. Remove the leaves [4, 5, 3] from the tr

[leetcode]_Maximum Depth of Binary Tree

第三道树的题目,我还是不会,我擦,怎么递归算法还是不能很好理解.看来还得好好研究下递归算法. 题目:求一棵树的最大深度. 思路:递归地求取左子树最大深度 和 右子树最大深度,返回较大值即为 整棵树的 最大深度. 代码: public int maxDepth(TreeNode root) { if(root == null) return 0; int leftHeight = 1,rightHeight = 1; if(root.left != null) leftHeight += maxD

Leetcode: Serialize and Deserialize Binary Tree

Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another comput

[leetcode]Minimum Depth of Binary Tree @ Python

原题地址:http://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ 题意: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 解题思路:分几种情况考虑:1,树为空,

LeetCode——Serialize and Deserialize Binary Tree

Description: Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or a

LeetCode: Maximum Depth of Binary Tree 题解

Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 题解: 题意比较清楚, 找到从root出发最长的一条路径的长度. 采用DFS即可. 相似的一道题: Minimux Depth of Binary Tree 解法: http://

leetcode题解:Construct Binary Tree from Inorder and Postorder Traversal(根据中序和后序遍历构造二叉树)

题目: Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 说明: 1)实现与根据先序和中序遍历构造二叉树相似,题目参考请进 算法思想 中序序列:C.B.E.D.F.A.H.G.J.I 后序序列:C.E.F.D.B.H.J.I.G.A 递归思路: 根据后序遍历的特点,知道后序

LeetCode——Maximum Depth of Binary Tree

Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 此题和求二叉树的最短路径几乎一模一样. public int maxDepth(TreeNode root) { if (root == null) return 0; int a

leetcode第一刷_Balanced Binary Tree

二叉平衡树好火啊,几乎每个公司的笔试题里都有它,考了好多次我都不会,挂笔试很有可能就是因为它,还有一个它的同伙叫二叉搜索树,貌似人气比它还要高一些.二叉平衡树是什么样的树呢,是每个节点的左右子树高度相差绝对值都不超过1.好,你说你终于回了,这不很简单吗,求一下根节点的左右字数高度,如果满足,他就是,否则就不是嘛.不是啊亲,要求是所有节点都满足这个条件,判断的时候必须每个节点都验证的! 扯了这么长,其实看看代码就明白了,怎么有种在贴吧发言要凑够15字的感觉. int getHeight(TreeN