【leetcode】114. Flatten Binary Tree to Linked List【java】
2017-01-08 16:22
211 查看
Given a binary tree, flatten it to a linked list in-place.
For example,
Given
The flattened tree should look like:
click to show hints.
Hints:
If you notice carefully in the flattened tree, each node's right child points to the next node of a pre-order traversal.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
//DFS:使用栈
/*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;
}
}
}*/
public class Solution {
private TreeNode lastNode = null;
public void flatten(TreeNode root) {
if (root == null) {
return;
}
if (lastNode != null) {
lastNode.left = null;
lastNode.right = root;
}
lastNode = root;
TreeNode left = root.left;
TreeNode right = root.right;
flatten(left);
flatten(right);
}
}
For example,
Given
1 / \ 2 5 / \ \ 3 4 6
The flattened tree should look like:
1 \ 2 \ 3 \ 4 \ 5 \ 6
click to show hints.
Hints:
If you notice carefully in the flattened tree, each node's right child points to the next node of a pre-order traversal.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
//DFS:使用栈
/*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;
}
}
}*/
public class Solution {
private TreeNode lastNode = null;
public void flatten(TreeNode root) {
if (root == null) {
return;
}
if (lastNode != null) {
lastNode.left = null;
lastNode.right = root;
}
lastNode = root;
TreeNode left = root.left;
TreeNode right = root.right;
flatten(left);
flatten(right);
}
}
相关文章推荐
- [Leetcode] Longest Palindromic Substring (Java)
- LeetCode:Reorder List C++与Java实现
- [Leetcode] Roman to Integer (Java)
- [Leetcode] Divide Two Integers (Java)
- [Leetcode] Search in Rotated Sorted Array (Java)
- [Leetcode] Substring with Concatenation of All Words (Java)
- [Leetcode] Integer to Roman (Java)
- [Leetcode] Remove Duplicates from Sorted Array (Java)
- [Leetcode] Implement strStr() (Java)
- LeetCode: Two Sum (Java)
- [Leetcode] Reverse Integer (Java)
- [Leetcode] Generate Parentheses (Java)
- [Leetcode] Longest Substring Without Repeating Characters (Java)
- [Leetcode] String to Integer (atoi) (Java)
- 【leetcode Java】二叉树的递归遍历以及最大深度的求解(Java)
- [Leetcode] Two Sum (Java)
- [Leetcode] ZigZag Conversion (Java)
- [Leetcode] Swap Nodes in Pairs (Java)
- [Leetcode] Longest Valid Parentheses (Java)
- [Leetcode] Search for a Range (Java)