美文网首页
二叉树4个题目

二叉树4个题目

作者: 啊磊11 | 来源:发表于2021-03-22 22:12 被阅读0次

//二叉搜索树的公共祖先

public static TreeNodetask40(TreeNode root, TreeNode node1, TreeNode node2){

if (root ==null){

return root;

    }

if(node1.valueroot.value){

return root;

    }

if (node1.value < root.value && node2.value

return task40(root.leftchild,node1,node2);

    }

if(node1.value>root.value && node2.value >root.value){

return task40(root.rightchild,node1,node2);

    }

return null;

}

//二叉树的公共祖先

public static TreeNodetask41(TreeNode root, TreeNode node1, TreeNode node2){

if(root ==null|| node1 == root || node2 == root){

return root;

    }

TreeNode left =task41(root.leftchild,node1,node2);

    TreeNode right =task41(root.rightchild,node1,node2);

    if(left !=null && right !=null){

return root;

    }

return left !=null?left:right;

}

public static ArrayListrest =new ArrayList();

public static Listtask42(TreeNode root, int target){

back(root,target,new ArrayList());

    return rest;

}

public static  void back(TreeNode root, int target, ArrayList path){

target = target -root.value;

    path.add(root.value);

    if(target ==0 && root.leftchild ==null && root.rightchild ==null){

rest.add(new ArrayList<>(path));

    }

if(root.leftchild !=null){

back(root.leftchild,target,path);

        path.remove(path.size() -1);

    }

if(root.rightchild !=null){

back(root.rightchild,target,path);

        path.remove(path.size() -1);

    }

}

//二叉树中的列表

public static boolean task43(TreeNode root, LinkedLIST head){

if (head ==null){

return true;

    }

if(root ==null){

return false;

    }

return task(root,head) ||task43(root.leftchild,head) ||task43(root.rightchild,head);

}

public static boolean task(TreeNode root, LinkedLIST head){

if (head ==null){

return true;

    }

if(root ==null){

return false;

    }

if(root.value != head.value){

return false;

    }else{

return task(root.leftchild,head.next)||task(root.rightchild,head.next);

    }

}

相关文章

  • JZ-018-二叉树的镜像

    二叉树的镜像 题目描述 操作给定的二叉树,将其变换为源二叉树的镜像。题目链接: 二叉树的镜像[https://ww...

  • 剑指 offer 学习之平衡二叉树

    题目描述 输入一棵二叉树,判断该二叉树是否是平衡二叉树。 题目链接:牛客网 解题思路 测试结果

  • [二叉树] 监控二叉树(困难)

    前言 今天的题目是一道将贪心和动态规划融合在二叉树的题目。 题目 监控二叉树[https://leetcode-c...

  • 27、28:二叉树的镜像

    习惯github pages风格的请看我的另一篇博客 题目27: 二叉树的镜像 对称的二叉树 题目:二叉树的镜像 ...

  • 平衡二叉树(java)

    题目描述: 输入一棵二叉树,判断该二叉树是否是平衡二叉树。 分析: 把这道题目放在二叉树的深度一题后面,真的是让人...

  • 二叉树的镜像

    题目来源: 牛客网--二叉树的镜像 题目描述 操作给定的二叉树,将其变换为源二叉树的镜像 输入描述 解题思路 首先...

  • [LeetCode By Go 27]538. Convert

    二叉树的题目挺多的,写了个初始化二叉树的函数,以后用 题目 Given a Binary Search Tree ...

  • LeetCode 894 All Possible Full B

    题目 all-possible-full-binary-trees 题目描述 满二叉树是一类二叉树,其中每个结点恰...

  • 构建求和树——二叉树的构建及遍历

    一、相关概念 二、题目 题目 思路 利用二叉树的前序、中序遍历序列构建二叉树,并遍历构建好的二叉树。 利用递归的思...

  • 19 二叉树的镜像

    二叉树的镜像 题目描述: 操作给定的二叉树,将其变换为源二叉树的镜像。 解题思路: 跟之前的题目有些类似的地方:看...

网友评论

      本文标题:二叉树4个题目

      本文链接:https://www.haomeiwen.com/subject/exnthltx.html