每日一题:1305. 两棵二叉搜索树中的所有元素

解题思路

搜索树中序遍历即为升序排列,对两个根进行dfs在把序列组合

代码

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<Integer> getAllElements(TreeNode root1, TreeNode root2) {
        List<Integer> res = new ArrayList<>();
        List<Integer> t1 = new ArrayList<>();
        if (root1 != null){
            dfs(root1,t1);
        }
        List<Integer> t2 = new ArrayList<>();
        if (root2 != null){
            dfs(root2,t2);
        }
        int index1 = 0,index2 = 0;
        while (index1 != t1.size() && index2 != t2.size()){
            final int n1 = t1.get(index1);
            final int n2 = t2.get(index2);
            if (n1 < n2){
                res.add(n1);
                index1++;
            }
            else if (n1 > n2){
                res.add(n2);
                index2++;
            }
            else if(n1 == n2){
                res.add(n1);
                res.add(n2);
                index1++;
                index2++;
            }
        }
        if (index1 != t1.size()){
            while (index1 != t1.size()){
                res.add(t1.get(index1));
                index1++;
            }
        }
        if (index2 != t2.size()){
            while (index2 != t2.size()){
                res.add(t2.get(index2));
                index2++;
            }
        }
        return res;
    }
    public void dfs(TreeNode root, List<Integer> list){
        if (root.left != null){
            dfs(root.left,list);
        }
        list.add(root.val);
        if (root.right != null){
            dfs(root.right,list);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我来解答你的问题。 对于这个问题,我们可以按照以下步骤来解决: 步骤1:构建二叉搜索树 我们可以使用给定的数据元素来构建一棵二叉搜索树,具体构建方法可以参考二叉搜索树的构建方法。 步骤2:遍历二叉搜索树 同样按照二叉搜索树的遍历方法,对构建好的二叉搜索树进行遍历,可选择前序遍历、序遍历或后序遍历。 步骤3:转换为双链表 对于二叉搜索树转换为双链表,我们可以使用序遍历,按照从小到大的顺序将树的节点连接起来。具体实现方法如下: 1. 定义一个全局变量pre,用来记录上一个节点 2. 对于每个节点,先将其左子树转换为双链表,然后将该节点与pre连接起来,更新pre为当前节点,最后将右子树转换为双链表 3. 最后返回双链表的头节点(即最左侧的节点) 步骤4:打印双链表 按照双链表的结构,我们可以从头节点开始遍历,依次输出每个节点的值。 步骤5:按正反序输出双链表的所有元素 对于双链表的正反序输出,我们可以定义两个指针,一个指向头节点,一个指向尾节点,然后分别从头尾开始遍历输出即可。 下面是示例代码,你可以参考一下: ```python # 定义二叉树节点 class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right # 构建二叉搜索树 def buildBST(nums): if not nums: return None mid = len(nums) // 2 root = TreeNode(nums[mid]) root.left = buildBST(nums[:mid]) root.right = buildBST(nums[mid+1:]) return root # 序遍历将二叉搜索树转换为双链表 def convertBSTtoDLL(root): global pre pre = None def inorder(root): nonlocal pre if not root: return None inorder(root.left) if pre: pre.right = root root.left = pre pre = root inorder(root.right) inorder(root) while root.left: root = root.left return root # 打印二叉树 def printTree(root): if not root: return None print(root.val) printTree(root.left) printTree(root.right) # 打印双链表 def printDLL(head): while head: print(head.val) head = head.right # 按正序输出双链表 def printDLLForward(head): while head: print(head.val) head = head.right # 按反序输出双链表 def printDLLBackward(head): while head.right: head = head.right while head: print(head.val) head = head.left # 测试代码 nums = [1, 2, 3, 4, 5, 6, 7] root = buildBST(nums) print("打印二叉树:") printTree(root) head = convertBSTtoDLL(root) print("打印双链表:") printDLL(head) print("按正序输出双链表:") printDLLForward(head) print("按反序输出双链表:") printDLLBackward(head) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值