好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

剑指Offer之Java算法习题精讲二叉树专项训练

题目一

二叉树题——查找二叉树

根据给定的二叉树根节点和指定条件查找其中指定元素

具体题目如下

 解法

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

/**

  * 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 {

     int i = 0 ;

     int res = 0 ;

     public int kthSmallest(TreeNode root, int k) {

         method(root,k);

         return res;

     }

      public void method(TreeNode root, int k){

         if (root== null ) return ;

         method(root.left,k);

         i++;

         if (i==k){

             res = root.val;

             return ;

         }

         method(root.right,k);

      }

}

题目二

二叉树题——转换累加树

根据给定的二叉树根节点按指定条件转换为累加树

具体题目如下

 解法

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

/**

  * 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 {

     int sum = 0 ;

     public TreeNode convertBST(TreeNode root) {

         method(root);

         return root;

     }

     public void method(TreeNode root) {

         if (root== null ){

             return ;

         }

         method(root.right);

         sum+=root.val;

         root.val = sum;

         method(root.left);

 

     }

}

题目三

二叉树题——验证二叉树

根据给定的二叉树根节点判断它是不是有效的二叉搜索树

具体题目如下

 解法

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

/**

  * 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 boolean isValidBST(TreeNode root) {

         return method(root, null , null );

     }

     public boolean method(TreeNode root,TreeNode min,TreeNode max){

         if (root== null ) return true ;

         if (min!= null &&root.val<=min.val) return false ;

         if (max!= null &&root.val>=max.val) return false ;

         return method(root.left,min,root)&&method(root.right,root,max);

     }

}

题目四

二叉树题——搜索二叉树

根据给定的二叉树根节点查找其中指定的数值

具体题目如下

解法

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

/**

  * 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 TreeNode searchBST(TreeNode root, int val) {

         if (root== null ) return null ;

         if (root.val==val) return root;

         if (root.val>=val){

             return searchBST(root.left,val);

         }

         if (root.val<val){

             return searchBST(root.right,val);

         }

         return null ;

     }

}

题目五

二叉树题——二叉树操作

根据给定的二叉树根节点将指定的数值插入二叉树

具体题目如下

 解法

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

/**

  * 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 TreeNode insertIntoBST(TreeNode root, int val) {

         return   method(root,val);

     }

     public TreeNode method(TreeNode root, int val){

         if (root== null ) return new TreeNode(val);

         if (root.val < val)

             root.right = insertIntoBST(root.right, val);

         if (root.val > val)

             root.left = insertIntoBST(root.left, val);

         return root;

     }

}

题目六

二叉树题——二叉树操作

根据给定的二叉树根节点在不改变二叉树性质的条件下删除其中指定的数值对应的节点

具体题目如下

算法

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

/**

  * 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 TreeNode deleteNode(TreeNode root, int key) {

         if (root == null ) return null ;

         if (root.val == key){

             if (root.left == null ) return root.right;

             if (root.right == null ) return root.left;

             TreeNode minNode = getMin(root.right);

             root.right = deleteNode(root.right, minNode.val);

             minNode.left = root.left;

             minNode.right = root.right;

             root = minNode;

         } else if (root.val>key){

             root.left = deleteNode(root.left,key);

         } else {

             root.right = deleteNode(root.right,key);

         }

         return root;

     }

     TreeNode getMin(TreeNode node) {

         while (node.left != null ) node = node.left;

         return node;

     }  

}

到此这篇关于剑指Offer之Java算法习题精讲二叉树专项训练的文章就介绍到这了,更多相关Java 二叉树内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://blog.csdn.net/wai_58934/article/details/123095807

查看更多关于剑指Offer之Java算法习题精讲二叉树专项训练的详细内容...

  阅读:19次