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);
        }
  • 相关阅读:
    班课2
    班课2
    班课1
    lecture 2
    lecture 1
    使用node的fs读取文件
    使用Node.js搭建一个本地服务器
    Node.js前言
    简述ES6其他的东西
    ES6异步操作Thunk、co和async
  • 原文地址:https://www.cnblogs.com/RazerLu/p/3552484.html
Copyright © 2011-2022 走看看