面试题 17.12.BiNode
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |
题目来源
题目网址面试题 17.12. BiNode - 力扣LeetCode
解题思路
中序遍历二叉树的同时将上一个节点的右节点指向当前节点将当前节点的左节点置空即可。
解题代码
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode convertBiNode(TreeNode root) {
if(root==null){
return root;
}
TreeNode res=new TreeNode();//头节点为空
convert(root,res);
return res.right;
}
public TreeNode convert(TreeNode from, TreeNode to){
if(from.left!=null){
to=convert(from.left,to);
}
to.right=from;
from.left=null;
to=to.right;
if(from.right!=null){
to=convert(from.right,to);
}
return to;
}
}
总结
无官方题解。