反转二叉树

反转二叉树

输入一个二叉树,输出其镜像。

如下图,即交换所有节点的左右子树。

\

 

这里提供两种思路:使用递归和不使用递归。

使用的二叉树定义如下:

 

1
2
3
4
5
6
7
8
9
10
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;
 
    public TreeNode(int val) {
        this.val = val;
 
    }
}


解决方法:

 

 

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import java.util.LinkedList;
import java.util.Scanner;
 
/*
 * 题目描述:输入一个二叉树,输出其镜像。
 * */
public class Solution {
    Scanner scanner = new Scanner(System.in);
     
    // 建立二叉树
    public TreeNode createTree(TreeNode root) {
        String val;
        val = scanner.next(); // next方法每次取到一个间隔符前面的数据
        if(val.equals("#")) {
            return null;
        }
        root = new TreeNode(Integer.parseInt(val));  System.out.println("输入的数据为:" + val);
        root.left = createTree(root.left);
        root.right = createTree(root.right);
        return root;
    }
    // 得到二叉树的镜像  —— 递归的方式
    public void Mirror(TreeNode root) {
        if(root == null) {
            return;
        }
        if((root.left == null) && (root.right == null)) {
            return;
        }
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
        Mirror(root.left);
        Mirror(root.right);
    }
    // 得到二叉树的镜像 —— 不使用递归
    public void MirrorNotRecursive(TreeNode root) {
        java.util.LinkedList stack = new java.util.LinkedList();
        TreeNode temp = null;
        if(root == null) {
            return;
        }
        stack.add(root);
        while(stack.size() != 0) {
            TreeNode node = stack.removeFirst();
            temp = node.left;
            node.left = node.right;
            node.right = temp;
            if(node.right != null) {
                stack.add(node.right);
            }
            if(node.left != null) {
                stack.add(node.left);
            }
        }
    }
     
    // 层次遍历二叉树
    public void levelTraverse(TreeNode root) {
        if (root == null) {
            return;
        }
        LinkedList list = new LinkedList();
        list.add(root);
        while (list.size() != 0) {
            TreeNode node = list.removeFirst(); // list.removeFirst() 该方法LinkedList才有
            System.out.print(node.val + " ");
            if(node.left != null) {
                list.add(node.left);
            }
            if(node.right != null) {
                list.add(node.right);
            }
        }
    }
     
    public static void main(String[] args) {
        Solution solution = new Solution();
        TreeNode root = null;
        root = solution.createTree(root);
        System.out.println("原二叉树的层次遍历");
        solution.levelTraverse(root);
        solution.Mirror(root);
        System.out.println("\n输出该二叉树的镜像");
        solution.levelTraverse(root);
        solution.MirrorNotRecursive(root);
        System.out.println("\n输出该二叉树的镜像(非递归方式)");
        solution.levelTraverse(root);
    }
}
 
/*
 * 测试数据:
 * 1 2 3 # 4 # # 5 6 # # # 7 8 # # 9 10 # # 11 # #  (说明:其中#说明左右子树为空)
 * 用先序遍历来建立树后,层次遍历结果为: 1 2 7 3 5 8 9 4 6 10 11
 * 反转二叉树之后:1 7 2 9 8 5 3 11 10 6 4
 */

算法之:翻转二叉树

 
 

事情大概是说,Max Howell 去 Google 面试,面试官说:虽然在 Google 有 90% 的工程师用你写的 Homebrew,但是你居然不能在白板上写出翻转二叉树的代码,所以滚蛋吧。

 


/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public TreeNode invertTree(TreeNode root) { if (root == null) {//一定要检查指针为空 return null; } root.left = invertTree(root.left); root.right = invertTree(root.right); TreeNode tmp = root.left; root.left = root.right; root.right = tmp; return root; } }


那么这道题考查了什么呢?我觉得主要是考查了递归的思想。递归是程序设计的精髓,掌握了他可以将一个大问题分解成小问题,继而求解。比如对于此题来说,反转一个二叉树其实就是:

  1. 反转二叉树的左右子树
  2. 将左右子树交换

而第 1 步又是一个反转二叉树的问题,所以就可以用递归来处理了。然后再考虑好递归的结束条件,这道题就可以解决了。

 

考查算法题会不会太片面?

是的,算法在工作中运用得比较少,一般人也不会在工作中专门积累解算法题的能力。所以通常做算法题最强的是应届生,因为他们有时间去刷题。而在实际 工作中,很多能力,都比算法能力重要。比如对代码的洁癖,对设计模式的理解,对计算机底层(操作系统、网络)的理解,对待工作的态度等。

那么,为什么那么多公司不考查,或者不重点考查这些,而只是问算法题呢?

答案很简单,因为要在几个小时内了解一个人太难了。通常一场面试只有一个小时,如果要在一个小时内把上面说到的知识点都面面俱到的考查,是做不到 的。但是如果花几天来考查,对于公司和求职者都是不可接受的。所以,很多大公司就用面算法题的方式来偷懒,因为这种方式虽然可能会误伤很多优秀的人才,但 是不合格的人,要混进去还是相当困难的。

讲一句不好听的,很多公司在招聘的时候,首先根据学校,把非 985 的学校简历都过滤掉,也是一种不太合理,但是有效的方法。清华北大的人一定就牛逼吗?不一定,但是从概率上讲,比南翔技校的人通过面试的机会要高一些。所 以,为了提高面试效率和成本,就直接把一般学校的简历过滤掉,这是一个不人道,但是从经济上合理的行为。

 

所以熟悉算法题是必须的。

posted @ 2018-09-06 13:07  混沌的StoRv  阅读(685)  评论(0)    收藏  举报