[抄题]:

Given a binary tree, return the values of its boundary in anti-clockwise direction starting from root. Boundary includes left boundary, leaves, and right boundary in order without duplicate nodes.

Left boundary is defined as the path from root to the left-most node. Right boundary is defined as the path from root to the right-most node. If the root doesn't have left subtree or right subtree, then the root itself is left boundary or right boundary. Note this definition only applies to the input binary tree, and not applies to any subtrees.

The left-most node is defined as a leaf node you could reach when you always firstly travel to the left subtree if exists. If not, travel to the right subtree. Repeat until you reach a leaf node.

The right-most node is also defined by the same way with left and right exchanged.

Example 1

Input:
1
\
2
/ \
3 4 Ouput:
[1, 3, 4, 2] Explanation:
The root doesn't have left subtree, so the root itself is left boundary.
The leaves are node 3 and 4.
The right boundary are node 1,2,4. Note the anti-clockwise direction means you should output reversed right boundary.
So order them in anti-clockwise without duplicates and we have [1,3,4,2].

Example 2

Input:
____1_____
/ \
2 3
/ \ /
4 5 6
/ \ / \
7 8 9 10 Ouput:
[1,2,4,7,8,9,10,6,3] Explanation:
The left boundary are node 1,2,4. (4 is the left-most node according to definition)
The leaves are node 4,7,8,9,10.
The right boundary are node 1,3,6,10. (10 is the right-most node).
So order them in anti-clockwise without duplicate nodes we have [1,2,4,7,8,9,10,6,3].

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

想不到“边界”应该怎么控制:用boolean变量表示方向l/r

主函数中设置t f,强制性设置好条件往左右扩展。dfs函数中设置== null, 有条件才往左右扩展,能走多深走多深。

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. dfs一直是对当前节点操作的,不是node.left/node.right
  2. 因为dfs的函数是左右分开的,不存在一了百了的情况。所以主函数也要一个点先进去,然后再进行dc。

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

想不到“边界”应该怎么控制:用boolean变量表示方向l/r

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

solution要用来新建answer对象。然后TreeNode root需要在主函数中再声明一遍。

class MyCode {
public static void main (String[] args) {
Solution answer = new Solution();
answer.root = new TreeNode(1);
answer.root.right = new TreeNode(2);
answer.root.right.left = new TreeNode(3);
answer.root.right.right = new TreeNode(4); List<Integer> result = answer.boundaryOfBinaryTree(answer.root);
for (int i = 0; i < result.size(); i++)
System.out.println("result.get(i) = " + result.get(i));
}

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

[潜台词] :

// package whatever; // don't place package name!

import java.io.*;
import java.util.*;
import java.lang.*; class TreeNode
{
int val;
TreeNode left, right; //parameter is another item
TreeNode(int item) {
val = item;
left = right = null;
}
} class Solution {
TreeNode root; public List<Integer> boundaryOfBinaryTree(TreeNode root) {
//initialization
List<Integer> result = new ArrayList<Integer>();
//corner case
if (root == null) return result;
//dfs in left and right
result.add(root.val);
dfs(root.left, true, false, result);
dfs(root.right, false, true, result);
//return
return result;
} public void dfs(TreeNode root, boolean lb, boolean rb, List<Integer> result) {
//exit case
if (root == null) return ;
//add the left root
if (lb) result.add(root.val);
//add the mid root
if (!lb && !rb && root.left == null && root.right == null)
result.add(root.val);
//dfs in left and right
dfs(root.left, lb, rb && root.left == null, result);
dfs(root.right, lb && root.left == null, rb, result);
//add the right root
if (rb) result.add(root.val);
}
} class MyCode {
public static void main (String[] args) {
Solution answer = new Solution();
answer.root = new TreeNode(1);
answer.root.right = new TreeNode(2);
answer.root.right.left = new TreeNode(3);
answer.root.right.right = new TreeNode(4); List<Integer> result = answer.boundaryOfBinaryTree(answer.root);
for (int i = 0; i < result.size(); i++)
System.out.println("result.get(i) = " + result.get(i));
}
}

545. Boundary of Binary Tree二叉树的边界的更多相关文章

  1. [LeetCode] 545. Boundary of Binary Tree 二叉树的边界

    Given a binary tree, return the values of its boundary in anti-clockwise direction starting from roo ...

  2. [LeetCode] Boundary of Binary Tree 二叉树的边界

    Given a binary tree, return the values of its boundary in anti-clockwise direction starting from roo ...

  3. Leetcode 110 Balanced Binary Tree 二叉树

    判断一棵树是否是平衡树,即左右子树的深度相差不超过1. 我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树 /** * Def ...

  4. [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)

    [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...

  5. [LeetCode] 111. Minimum Depth of Binary Tree 二叉树的最小深度

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  6. [LeetCode] 543. Diameter of Binary Tree 二叉树的直径

    Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a b ...

  7. LeetCode - Boundary of Binary Tree

    Given a binary tree, return the values of its boundary in anti-clockwise direction starting from roo ...

  8. [LeetCode] Serialize and Deserialize Binary Tree 二叉树的序列化和去序列化

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  9. [LeetCode] Lowest Common Ancestor of a Binary Tree 二叉树的最小共同父节点

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

随机推荐

  1. Spark:读取mysql数据作为DataFrame

    在日常工作中,有时候需要读取mysql的数据作为DataFrame数据源进行后期的Spark处理,Spark自带了一些方法供我们使用,读取mysql我们可以直接使用表的结构信息,而不需要自己再去定义每 ...

  2. 重建二叉树(JAVA)

    重建二叉树 题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字. 例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历 ...

  3. PHP的 preg_match_all

    语法:int preg_match_all ( string pattern, string subject, array &matches [, int flags] ) 这个函数的返回值是 ...

  4. jenkins构建完成后,执行的命令行的东西也会自动结束的解决办法

    问题: 把添加VPN的指令写在脚本里,然后用jenkins执行这个脚本,jenkins执行的结果是成功的,但是在机器上看,并没有执行成功.   问题分析: 其实在机器上执行过添加VPN的操作,只是在j ...

  5. 【转】C++中嵌入python程序——参数传递

    C++中嵌入python程序——参数传递 前面两篇博客已经介绍如何在C++中嵌套使用 python,但是在实际使用中,我们需要向python传递各种各样的参数,这样的程序才具有更高的灵活性.下面简单介 ...

  6. Delphi2009之TImage

    TPngImage原来是SourceFroge上的一个开源项目,现在突然消失了,为什么呢?Nick 在他的博客上写到:TPNGImage被CodeGear/Embarcadero收购了,现在直接就是D ...

  7. Maya中输出alembic文件的方法

    Maya中输出alembic文件是有现成api调用的,与maya中大部分api一样,这个功能参数的传入是非常类似mel的,本质上讲都是kwargs类型的参数,所以我们传入的参数就需要整理成类似于mel ...

  8. SpringBoot入门(0) HelloWorld的实现与原理分析

    SpringBoot(0) HelloWorld的实现与原理分析 一.环境准备 1.1 环境约束 –jdk1.8:Spring Boot 推荐jdk1.7及以上:java version “1.8.0 ...

  9. Python数字(Number)

    Python 数字数据类型用于存储数值. 数据类型是不允许改变的,这就意味着如果改变数字数据类型得值,将重新分配内存空间. 以下实例在变量赋值时 Number 对象将被创建:var1 = 1var2 ...

  10. vue展示dicom文件,医疗系统。

    环境:vue.webpack.constone 资料来源及文件:https://github.com/GleasonBian/CornerstoneVueWADO 需要下载的模块:cornerston ...