zoukankan      html  css  js  c++  java
  • LeetCode 872. Leaf-Similar Trees

    原题链接在这里:https://leetcode.com/problems/leaf-similar-trees/

    题目:

    Consider all the leaves of a binary tree.  From left to right order, the values of those leaves form a leaf value sequence.

    For example, in the given tree above, the leaf value sequence is (6, 7, 4, 9, 8).

    Two binary trees are considered leaf-similar if their leaf value sequence is the same.

    Return true if and only if the two given trees with head nodes root1 and root2 are leaf-similar.

    Note:

    • Both of the given trees will have between 1 and 100 nodes.

    题解:

    Use pre-order to get leaf one by one and compare the value.

    Time Complexity: O(n). n is tree size.

    Space: O(h). h is tree height.

    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 class Solution {
    11     public boolean leafSimilar(TreeNode root1, TreeNode root2) {
    12         if(root1 == null || root2 == null){
    13             return root1 == root2;
    14         }
    15         
    16         Stack<TreeNode> stk1 = new Stack<>();
    17         Stack<TreeNode> stk2 = new Stack<>();
    18         stk1.push(root1);
    19         stk2.push(root2);
    20         while(!stk1.isEmpty() && !stk2.isEmpty()){
    21             int leaf1 = getLeafVal(stk1);
    22             int leaf2 = getLeafVal(stk2);
    23             if(leaf1 != leaf2){
    24                 return false;
    25             }
    26         }
    27         
    28         return stk1.isEmpty() && stk2.isEmpty();
    29     }
    30     
    31     private int getLeafVal(Stack<TreeNode> stk){
    32         while(!stk.isEmpty()){
    33             TreeNode cur = stk.pop();
    34             if(cur.right != null){
    35                 stk.push(cur.right);
    36             }
    37             
    38             if(cur.left != null){
    39                 stk.push(cur.left);
    40             }
    41             
    42             if(cur.left == null && cur.right == null){
    43                 return cur.val;
    44             }
    45         }
    46         
    47         return -1;
    48     }
    49 }

    AC JavaScript:

     1 /**
     2  * Definition for a binary tree node.
     3  * function TreeNode(val) {
     4  *     this.val = val;
     5  *     this.left = this.right = null;
     6  * }
     7  */
     8 /**
     9  * @param {TreeNode} root1
    10  * @param {TreeNode} root2
    11  * @return {boolean}
    12  */
    13 var leafSimilar = function(root1, root2) {
    14     if(!root1 || !root2){
    15         return roo1 == root2;
    16     }
    17     
    18     var stk1 = [];
    19     var stk2 = [];
    20     stk1.push(root1);
    21     stk2.push(root2);
    22     
    23     while(stk1.length != 0 && stk2.length != 0){
    24         var leaf1 = getLeaf(stk1);
    25         var leaf2 = getLeaf(stk2);
    26         if(leaf1 != leaf2){
    27             return false;
    28         }
    29     }
    30     
    31     return stk1.length == 0 && stk2.length == 0;
    32 };
    33 
    34 var getLeaf = function(stk){
    35     while(stk.length != 0){
    36         var cur = stk.pop();
    37         if(cur.right){
    38             stk.push(cur.right);
    39         }
    40         
    41         if(cur.left){
    42             stk.push(cur.left);
    43         }
    44         
    45         if(!cur.left && !cur.right){
    46             return cur.val;
    47         }
    48     }
    49     
    50     return -1;
    51 }
  • 相关阅读:
    Linux常用操作命令大全
    判断系统,是否是移动端
    一款兼容小程序和web端的框架 Kbone-API
    defer 和 async的区别
    快应用多次弹出添加到桌面的bug
    js判断一个元素是否在数组中
    css 画太极
    css3锯齿
    js 获取屏幕宽度
    js 判断div距离浏览器顶部或者底部的距离
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/12131934.html
Copyright © 2011-2022 走看看