/**
* 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<>();
stack.push(root);
while(!stack.isEmpty()){
TreeNode node = stack.pop();
if(node.right != null){
stack.push(node.right);
}
if(node.left!=null){
stack.push(node.left);
}
if(!stack.isEmpty()){
node.right = stack.peek();
}
node.left = null;
}
}
}
[LeetCode] 114. Flatten Binary Tree to Linked List
最新推荐文章于 2022-09-06 16:04:14 发布