[leetcode]114. Flatten Binary Tree to Linked List

发布于:2021-06-11 01:51:09

就是简单的递归


/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public void flatten(TreeNode root) {
if(root==null) return;
flatten(root.left);
flatten(root.right);

TreeNode ptr = root.left;
if(ptr==null){
return;
}
while(ptr.right!=null){
ptr = ptr.right;
}

ptr.right = root.right;
root.right = root.left;
root.left = null;
return;

}


}

相关推荐

最新更新

猜你喜欢