算法-二叉树

2018/09/07 算法 Java 二叉树
  本文为「原创」内容,如需转载请注明出处!             
本文共 934 字,需 11 分钟阅读

背景

这是一个关于二叉树的一些算法题目,主要来源于 LeetCode

  1. 判断同一颗树,100. Same Tree
     Input:    1         1
              / \       / \
             2   3     2   3
    
            [1,2,3],   [1,2,3]
    
     Output: true
    
  2. 判断是否为子树,572. Subtree of Another Tree
    Given tree s:
    
           3
          / \
         4   5
        / \
       1   2
     Given tree t:
        4 
       / \
      1   2
     Return true, because t has the same structure and node values with a subtree of s.
    
  3. 树的直径,树的两个节点的最大距离,543. Diameter of Binary Tree
           1
          / \
         2   3
        / \     
       4   5  
    
     Return 3, which is the length of the path [4,2,1,3] or [5,2,1,3].
     Note: The length of path between two nodes is represented 
           by the number of edges between them.
    
  4. 树的路径和,找出所有路径和为给定值得总量 437. Path Sum III

    root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8
    
           10
          /  \
         5   -3
        / \    \
       3   2   11
      / \   \
     3  -2   1
     Return 3. The paths that sum to 8 are:
     1.  5 -> 3
     2.  5 -> 2 -> 1
     3. -3 -> 11
    

搜索

    文章目录