LeetCode(剑指 Offer)- 32 - III. 从上到下打印二叉树 III

Posted 放羊的牧码

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode(剑指 Offer)- 32 - III. 从上到下打印二叉树 III相关的知识,希望对你有一定的参考价值。

题目链接:点击打开链接

题目大意:

解题思路:

相关企业

  • 字节跳动

AC 代码

  • Java
/**
 * Definition for a binary tree node.
 * public class TreeNode 
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x)  val = x; 
 * 
 */

// 解决方案(1)
class Solution 
    public List<List<Integer>> levelOrder(TreeNode root) 
        Queue<TreeNode> queue = new LinkedList<>();
        List<List<Integer>> res = new ArrayList<>();
        Stack<Integer> stack = new Stack<>();
        if(root != null) queue.add(root);
        boolean reverse = false;
        while(!queue.isEmpty()) 
            List<Integer> tmp = new ArrayList<>();
            for(int i = queue.size(); i > 0; i--) 
                TreeNode node = queue.poll();
                if (reverse) 
                    stack.push(node.val);
                 else 
                    tmp.add(node.val);
                
                if(node.left != null) queue.add(node.left);
                if(node.right != null) queue.add(node.right);
            

            if (reverse) 
                while (!stack.isEmpty()) 
                    tmp.add(stack.pop());
                
            
            reverse = !reverse;
            res.add(tmp);
        
        return res;
    


// 解决方案(2)
class Solution 
    public List<List<Integer>> levelOrder(TreeNode root) 
        Queue<TreeNode> queue = new LinkedList<>();
        List<List<Integer>> res = new ArrayList<>();
        if(root != null) queue.add(root);
        while(!queue.isEmpty()) 
            LinkedList<Integer> tmp = new LinkedList<>();
            for(int i = queue.size(); i > 0; i--) 
                TreeNode node = queue.poll();
                if(res.size() % 2 == 0) tmp.addLast(node.val);
                else tmp.addFirst(node.val);
                if(node.left != null) queue.add(node.left);
                if(node.right != null) queue.add(node.right);
            
            res.add(tmp);
        
        return res;
    


// 解决方案(3)
class Solution 
    public List<List<Integer>> levelOrder(TreeNode root) 
        Deque<TreeNode> deque = new LinkedList<>();
        List<List<Integer>> res = new ArrayList<>();
        if(root != null) deque.add(root);
        while(!deque.isEmpty()) 
            // 打印奇数层
            List<Integer> tmp = new ArrayList<>();
            for(int i = deque.size(); i > 0; i--) 
                // 从左向右打印
                TreeNode node = deque.removeFirst();
                tmp.add(node.val);
                // 先左后右加入下层节点
                if(node.left != null) deque.addLast(node.left);
                if(node.right != null) deque.addLast(node.right);
            
            res.add(tmp);
            if(deque.isEmpty()) break; // 若为空则提前跳出
            // 打印偶数层
            tmp = new ArrayList<>();
            for(int i = deque.size(); i > 0; i--) 
                // 从右向左打印
                TreeNode node = deque.removeLast();
                tmp.add(node.val);
                // 先右后左加入下层节点
                if(node.right != null) deque.addFirst(node.right);
                if(node.left != null) deque.addFirst(node.left);
            
            res.add(tmp);
        
        return res;
    


// 解决方案(4)
class Solution 
    public List<List<Integer>> levelOrder(TreeNode root) 
        Queue<TreeNode> queue = new LinkedList<>();
        List<List<Integer>> res = new ArrayList<>();
        if(root != null) queue.add(root);
        while(!queue.isEmpty()) 
            List<Integer> tmp = new ArrayList<>();
            for(int i = queue.size(); i > 0; i--) 
                TreeNode node = queue.poll();
                tmp.add(node.val);
                if(node.left != null) queue.add(node.left);
                if(node.right != null) queue.add(node.right);
            
            if(res.size() % 2 == 1) Collections.reverse(tmp);
            res.add(tmp);
        
        return res;
    
  • C++
/**
 * Definition for a binary tree node.
 * struct TreeNode 
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) 
 * ;
 */

// 解决方案(1)
class Solution 
public:
    vector<vector<int>> levelOrder(TreeNode* root) 
        deque<TreeNode*> deque;
        vector<vector<int>> res;
        if(root != NULL) deque.push_back(root);
        while(!deque.empty()) 
            // 打印奇数层
            vector<int> tmp;
            for(int i = deque.size(); i > 0; i--) 
                // 从左向右打印
                TreeNode* node = deque.front();
                deque.pop_front();
                tmp.push_back(node->val);
                // 先左后右加入下层节点
                if(node->left != NULL) deque.push_back(node->left);
                if(node->right != NULL) deque.push_back(node->right);
            
            res.push_back(tmp);
            if(deque.empty()) break; // 若为空则提前跳出
            // 打印偶数层
            tmp.clear();
            for(int i = deque.size(); i > 0; i--) 
                // 从右向左打印
                TreeNode* node = deque.back();
                deque.pop_back();
                tmp.push_back(node->val);
                // 先右后左加入下层节点
                if(node->right != NULL) deque.push_front(node->right);
                if(node->left != NULL) deque.push_front(node->left);
            
            res.push_back(tmp);
        
        return res;
    
;

// 解决方案(2)
class Solution 
public:
    vector<vector<int>> levelOrder(TreeNode* root) 
        queue<TreeNode*> que;
        vector<vector<int>> res;
        if(root != NULL) que.push(root);
        while(!que.empty()) 
            vector<int> tmp;
            for(int i = que.size(); i > 0; i--) 
                TreeNode* node = que.front();
                que.pop();
                tmp.push_back(node->val);
                if(node->left != NULL) que.push(node->left);
                if(node->right != NULL) que.push(node->right);
            
            if(res.size() % 2 == 1) reverse(tmp.begin(),tmp.end());
            res.push_back(tmp);
        
        return res;
    
;

以上是关于LeetCode(剑指 Offer)- 32 - III. 从上到下打印二叉树 III的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode刷题Python剑指 Offer 32 - I. 从上到下打印二叉树

LeetCode(剑指 Offer)- 32 - II. 从上到下打印二叉树 II

Leetcode刷题Python剑指 Offer 32 - II. 从上到下打印二叉树 II

[LeetCode]剑指 Offer 32 - I. 从上到下打印二叉树

[LeetCode]剑指 Offer 32 - I. 从上到下打印二叉树

[LeetCode]剑指 Offer 32 - III. 从上到下打印二叉树 III