zoukankan      html  css  js  c++  java
  • Flatten Binary Tree to Linked List

    Given a binary tree, flatten it to a linked list in-place.

    For example,
    Given

             1
            / 
           2   5
          /    
         3   4   6
    

    The flattened tree should look like:

       1
        
         2
          
           3
            
             4
              
               5
                
                 6

    ref:http://www.cnblogs.com/feiling/p/3278639.html

    [解题思路]

             1
              
    2
    /
    3 4
    5 6
    对root的左子树进行处理,将左子树的根节点和左子树的右子树插入右子树中
    接下来对节点2进行处理,同样将2的左子树插入右子树中
    public void flatten(TreeNode root) {
            if(root == null){
                return;
            }
            
            if(root.left != null){
                TreeNode rightNode = root.right;
                TreeNode leftNode = root.left;
                root.left = null;
                root.right = leftNode;
                TreeNode p = leftNode;
                while(p.right != null){
                    p = p.right;
                }
                p.right = rightNode;
            }
            flatten(root.right);
        }
  • 相关阅读:
    go包初始化顺序
    go map
    go包管理
    C++ 线程池
    RAFT共识算法笔记
    最大子序列和
    常见网络攻击及其防御
    go常用标准库功能
    using代替typedef
    typename和class的区别
  • 原文地址:https://www.cnblogs.com/RazerLu/p/3552484.html
Copyright © 2011-2022 走看看