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 tree

1
/
2
2. Remove the leaf [2] from the tree

  1

3. Remove the leaf [1] from the tree

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

该题的重点是对tree进行剪枝活动时候,我们可以利用x = change(x) 在recursive函数中。例如, root.left = removeLeaves(root.left, result); root.right = removeLeaves(root.right, result), 这样下层内嵌函数返回的null值可以赋给上层调用函数的root.left/right。 因此递归函数的返回类型很自然应该设为TreeNode

 1 public class Solution {
 2     private TreeNode removeLeaves(TreeNode root, List<Integer> result)
 3     {
 4         if(root==null)  return null;
 5         if((root.left == null)&&(root.right==null)){
 6             result.add(root.val);
 7             return null;
 8         }
 9
10         root.left = removeLeaves(root.left, result);
11         root.right = removeLeaves(root.right, result);
12
13         return root;
14
15     }
16
17     public List<List<Integer>> findLeaves(TreeNode root) {
18         List<List<Integer>> res = new ArrayList<List<Integer>>();
19         if (root==null) return res;
20         while(root!=null){
21             List<Integer> leaves = new ArrayList<Integer>();
22             root = removeLeaves(root, leaves);
23             res.add(leaves);
24         }
25
26         return res;
27     }
28 }

1
3. Remove the leaf [1] from the tree

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

时间: 2024-08-02 07:59:02

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-面试算法经典-Java实现】【111-Minimum Depth of Binary Tree(二叉树的最小深度)】

[111-Minimum Depth of Binary Tree(二叉树的最小深度)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 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. 题目大意 给定

[LintCode] Invert Binary Tree 翻转二叉树

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. Example Given 4 points: (1,2), (3,6), (0,0), (1,3). The maximum number is 3. LeeCode上的原题,可参见我之前的博客Invert Binary Tree 翻转二叉树. 解法一: // Recursion class So

【LeetCode-面试算法经典-Java实现】【104-Maximum Depth of Binary Tree(二叉树的最大深度)】

[104-Maximum Depth of Binary Tree(二叉树的最大深度)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 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. 题目大意 给定

遍历二叉树 traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化

遍历二叉树   traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化 二叉树3个基本单元组成:根节点.左子树.右子树 以L.D.R分别表示遍历左子树.访问根节点.遍历右子树 可能的情况6种 排列A3 2 LDR LRD DLR DRL RLD RDL 若限定先左后右 LDR LRD  中根序遍历  后根序遍历 DLR  先根序遍历 先/中/后 序遍历 原文地址:https://www.cnblogs.com/yuanjiangw/p

Find Leaves of Binary Tree

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]

14.Diameter of Binary Tree(二叉树的直径)

Level: ??Easy 题目描述: Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root. Exam

Leetcode 257 Binary Tree Paths 二叉树 DFS

找到所有根到叶子的路径 深度优先搜索(DFS), 即二叉树的先序遍历. 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 p

lintcode 中等题:binary tree serialization 二叉树的序列化和反序列化

题目 二叉树的序列化和反序列化 设计一个算法,并编写代码来序列化和反序列化二叉树.将树写入一个文件被称为“序列化”,读取文件后重建同样的二叉树被称为“反序列化”. 如何反序列化或序列化二叉树是没有限制的,你只需要确保可以将二叉树序列化为一个字符串,并且可以将字符串反序列化为原来的树结构. 样例 给出一个测试数据样例, 二叉树{3,9,20,#,#,15,7},表示如下的树结构: 3 / 9 20 / 15 7 我们的数据是进行BFS遍历得到的.当你测试结果wrong answer时,你可以作为输