LeetCode 114. Flatten Binary Tree to Linked List
来源:互联网 发布:北京市水文站数据 编辑:程序博客网 时间:2024/06/06 01:34
description:
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
开始,我遇到了一个大问题,总是进入死循环。为题在于,遍历左子树的时候,root.left 将 root.right中的信息给覆盖了,因此程序进入不断压栈的状态。因此,应该在进行遍历左子树的时候,将本节点的右子树的信息进行保存。
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { TreeNode lastNode = null; public void flatten(TreeNode root) { if(root == null) { return; } if (lastNode != null) { lastNode.right = root; lastNode.left = null; } lastNode = root; TreeNode right = root.right; flatten(root.left); flatten(right); }}
0 0
- LeetCode: Flatten Binary Tree to Linked List
- LeetCode Flatten Binary Tree to Linked List
- LeetCode: Flatten Binary Tree to Linked List
- [Leetcode] Flatten Binary Tree to Linked List
- [LeetCode] Flatten Binary Tree to Linked List
- Leetcode: Flatten Binary Tree to Linked List
- leetcode Flatten Binary Tree to Linked List
- LeetCode Flatten Binary Tree to Linked List
- LeetCode - Flatten Binary Tree to Linked List
- 【leetcode】Flatten Binary Tree to Linked List
- [LeetCode]Flatten Binary Tree to Linked List
- [Leetcode]Flatten Binary Tree to Linked List
- [leetcode]Flatten Binary Tree to Linked List
- LeetCode-Flatten Binary Tree to Linked List
- [leetcode] Flatten Binary Tree to Linked List
- LeetCode - Flatten Binary Tree to Linked List
- LeetCode:Flatten Binary Tree to Linked List
- [LeetCode] Flatten Binary Tree to Linked List
- python 5-5 如何访问文件的状态os.stat()/os.path
- ThreadLocal用法简介
- 11.通用定时器—基于CT117E开发板的STM32库函数编程
- zookeeper节点Watch机制实例展示
- 可以让你嗨翻春节的创意开源工具
- LeetCode 114. Flatten Binary Tree to Linked List
- 即搜即得,首批 Android Instant Apps 开始测试
- JavaScript学习笔记之变量
- 1. web前端开发分享-css,js入门篇
- 神经网络学习笔记-01-基本概念
- LEETCODE--Reverse String
- 关于js中call(), apply(), bind()简单的解释
- 关于个人blog被黑的说明
- C语言 sizeof函数详解