zoukankan      html  css  js  c++  java
  • 二叉树展开为链表

    前序遍历+重赋值

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode() {}
     *     TreeNode(int val) { this.val = val; }
     *     TreeNode(int val, TreeNode left, TreeNode right) {
     *         this.val = val;
     *         this.left = left;
     *         this.right = right;
     *     }
     * }
     */
    class Solution {
        List<Integer> list = new ArrayList<>();
        public void flatten(TreeNode root) {
            if(root == null) return ;
            preOrder(root);
            int i = 0;
            while(i +1 < list.size()){
                root.left = null;
                root.right = new TreeNode(list.get(i+1));
                root = root.right;   
                i++;          
            }
    
        }
        public void preOrder(TreeNode root){
            if(root == null) return ;
            list.add(root.val);
            preOrder(root.left);
            preOrder(root.right);
        }
        
    }
    
    

  • 相关阅读:
    Spring MVC之视图呈现
    Spring MVC之HandlerMap 初始化
    Spring MVC之DispatcherServlet请求处理
    合成模式
    缺省适配器
    适配器模式
    原始模型
    克隆 和 比较
    建造模式
    线段树
  • 原文地址:https://www.cnblogs.com/cstdio1/p/13419748.html
Copyright © 2011-2022 走看看