zoukankan      html  css  js  c++  java
  • [LeetCode]617.Merge Two Binary Trees

    题目描述:

    Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not.

    You need to merge them into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of new tree.

    Example 1:

    Input: 
    	Tree 1                     Tree 2                  
              1                         2                             
             /                        /                             
            3   2                     1   3                        
           /                                                    
          5                             4   7                  
    Output: 
    Merged tree:
    	     3
    	    / 
    	   4   5
    	  /     
    	 5   4   7
    

    Note: The merging process must start from the root nodes of both trees.

    思路:

    给定两个二叉树,合并。
    规则是如果该位置两个树都有值,则将两树的值加起来,如果一个有一个没有,则将有的那个树的值赋值
    递归,用二叉树前序遍历,求每个位置的值,然后返回根节点。

     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 public class Solution617 {
    11      public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
    12             if(t1 == null && t2==null) return null;
    13             if(t1 == null && t2!= null) return t2;
    14             if(t1 != null && t2== null) return t1;
    15             if(t1 != null && t2!= null) {
    16                 t1.val += t2.val;
    17                 t1.left=mergeTrees(t1.left, t2.left);
    18                 t1.right=mergeTrees(t1.right, t2.right);
    19             }
    20             return t1;
    21         }
    22      public void shuchu(TreeNode treeNode){
    23         if(treeNode!=null){
    24              System.out.println(treeNode.val);
    25              shuchu(treeNode.left);
    26              shuchu(treeNode.right);
    27          }
    28      }
    29     public static void main(String[] args) {
    30         // TODO Auto-generated method stub
    31         Solution617 solution617 = new Solution617();
    32         TreeNode t1 = new TreeNode(1);
    33         t1.left = new TreeNode(3);
    34         t1.right = new TreeNode(2);
    35         t1.left.left = new TreeNode(5);
    36         TreeNode t2 = new TreeNode(2);
    37         t2.left = new TreeNode(1);
    38         t2.right = new TreeNode(3);
    39         t2.left.left = null;
    40         t2.left.right = new TreeNode(4);
    41         t2.right.left = null;
    42         t2.right.right = new TreeNode(7);
    43         TreeNode treeNode =solution617.mergeTrees(t1, t2);
    44         solution617.shuchu(treeNode);
    45     }
    46 
    47 }
  • 相关阅读:
    linux使用rm-rf删除之文件恢复
    性能测试常见瓶颈分析及调优方法
    性能压测QPS 和 TPS区别
    Linux查看系统性能
    Python中split()和split(" ")的区别
    Python-标准库之functools
    Python-内置函数_@propery、@classmethod、@staticmethod详解
    Python-类的内置方法中__repr__和__str__的区别
    Python-标准库之运算符替代函数Operator
    Python-标准库之迭代器-itertools
  • 原文地址:https://www.cnblogs.com/zlz099/p/8508046.html
Copyright © 2011-2022 走看看