zoukankan      html  css  js  c++  java
  • Tweaked Identical Binary Trees

    Determine whether two given binary trees are identical assuming any number of ‘tweak’s are allowed. A tweak is defined as a swap of the children of one node in the tree.

    Examples

            5

          /    

        3        8

      /  

    1      4

    and

            5

          /    

        8        3

               /  

              1     4

    the two binary trees are tweaked identical.

    How is the binary tree represented?

    We use the level order traversal sequence with a special symbol "#" denoting the null node.

    For Example:

    The sequence [1, 2, 3, #, #, 4] represents the following binary tree:

        1

      /  

     2     3

          /

        4

    time: O(4 ^ log_2(n)) = O(n ^ 2), space: O(h)

    /**
     * public class TreeNode {
     *   public int key;
     *   public TreeNode left;
     *   public TreeNode right;
     *   public TreeNode(int key) {
     *     this.key = key;
     *   }
     * }
     */
    public class Solution {
      public boolean isTweakedIdentical(TreeNode one, TreeNode two) {
        // Write your solution here
        if(one == null && two == null) {
          return true;
        } else if(one == null || two == null) {
          return false;
        }else if(one.key != two.key) {
          return false;
        }
        return isTweakedIdentical(one.left, two.right) && isTweakedIdentical(one.right, two.left) || isTweakedIdentical(one.left, two.left) && isTweakedIdentical(one.right, two.right);
      }
    }
  • 相关阅读:
    Flutter-路由
    写一个底部Tabs导航动态组件
    实例:Flutter布局01
    HTTP报文
    HTML文本的各种属性
    HTML必要简介和基础
    MySQL8.0.23安装超详细傻瓜式
    从一个HTTP请求来研究网络分层原理
    MySQL的增删改查
    MySQL常用数据类型有哪些?
  • 原文地址:https://www.cnblogs.com/fatttcat/p/10253373.html
Copyright © 2011-2022 走看看