[LeetCode]--114. Flatten Binary Tree to Linked List
来源:互联网 发布:网络在线教育平台 编辑:程序博客网 时间:2024/06/17 12:59
See the problem! LeetCode_114_Flatten_Binary_Tree
What’s more, keep in mind the requirement that flatten it to a linked list in place
See the answer code.
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public void flatten(TreeNode root) { if (root == null) return; Stack<TreeNode> stack = new Stack<TreeNode>(); stack.push(root); while (!stack.isEmpty()) { TreeNode cur = stack.pop(); if (cur.right != null) stack.push(cur.right); if (cur.left != null) stack.push(cur.left); if (!stack.isEmpty()) cur.right = stack.peek();`这里写代码片` cur.left = null; } }}
More important, don’t forget cur.left = null
,or you’ll get TLE
Reference:cbmbbz
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
- Customize export files in SSRS
- Cocos Creator 动作列表(摘自官方文档)
- Class.getSimpleName()的作用
- 记一个合并代码导致的错误(自定义控件路径改变引起找不到类)
- 小米Git问题-学习笔记
- [LeetCode]--114. Flatten Binary Tree to Linked List
- POJ 3162 Walking Race 树的直径+单调队列(其实暴力也可以>_<)
- mysql rollback与AI
- poj1185炮兵阵地(状态压缩DP)
- Android 6.0以下检测摄像头权限
- 压缩感知重构算法之迭代硬阈值(Iterative Hard Thresholding,IHT)
- C++系列——const,引用,指针用法小结
- Cocos Creator 使用计时器(摘自官方文档)
- 打包jar包