[LeetCode][Java] Binary Tree Zigzag Level Order Traversal

题目:

Given a binary tree, return the zigzag level order traversal of its nodes‘ values. (ie, from left to right, then right to left for the next level and alternate between).

For example:

Given binary tree {3,9,20,#,#,15,7},

    3
   /   9  20
    /     15   7

return its zigzag level order traversal as:

[
  [3],
  [20,9],
  [15,7]
]

题意:

给定一棵二叉树,返回这棵树的节点的‘Z‘字形遍历(即 起始顺序为先左后右,下一层的顺序为先右后左,这样交替进行)

比如

给定二叉树{3,9,20,#,#,15,7},

    3
   /   9  20
    /     15   7

返回这棵树的‘z‘字形遍历:

[
  [3],
  [20,9],
  [15,7]
]

算法分析:

利用题目《Binary Tree Level Order Traversal》的结果,对ArrayList中的奇数元素进行倒序 。难度不大,直接上代码

AC代码:

<span style="font-family:Microsoft YaHei;font-size:12px;">public class Solution
{
    public ArrayList<ArrayList<Integer>> zigzagLevelOrder(TreeNode root)
    {
         ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>();
        if (root == null)
            return list;  

         list = levelOrder(root);
         for(int i=1;i<list.size();i+=2)
         {
             reverse(list.get(i));
         }
		return list;
    }
         private static void reverse(ArrayList<Integer> temlist)
         {
        	 int tem=0;
        	 int temsize=0;
        	 if(temlist.size()%2==0)
        		 temsize=temlist.size()/2-1;
        	 else
        		 temsize=temlist.size()/2;
        	 for(int i=0;i<=temsize;i++)
		   {
			 tem=temlist.get(i);
			 temlist.set(i, temlist.get(temlist.size()-1-i));
			 temlist.set(temlist.size()-1-i, tem);
		   }

         }
	 public static ArrayList<ArrayList<Integer>> levelOrder(TreeNode root)
	 {
        ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
        if (root == null)
        {
            return res;
        }
        ArrayList<Integer> tmp = new ArrayList<Integer>();
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        queue.offer(root);
        int num;
        boolean reverse = false;
        while (!queue.isEmpty())
        {
            num = queue.size();  //每次通过这个确定最终的出队数目
            tmp.clear();
            for (int i = 0; i < num; i++) //队列中出1个父,进两个子;出2个父,进4个子;出4个父,进8个子
            {
                TreeNode node = queue.poll();
                tmp.add(node.val);
                if (node.left != null)
                    queue.offer(node.left);
                if (node.right != null)
                    queue.offer(node.right);
            }  

            res.add(new ArrayList<Integer>(tmp));
        }
        return res;
	  }
}</span>

版权声明:本文为博主原创文章,转载注明出处

时间: 2024-12-11 14:13:03

[LeetCode][Java] Binary Tree Zigzag Level Order Traversal的相关文章

Leetcode 树 Binary Tree Zigzag Level Order Traversal

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Binary Tree Zigzag Level Order Traversal Total Accepted: 8678 Total Submissions: 33047My Submissions Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from le

Java for LeetCode 103 Binary Tree Zigzag Level Order Traversal

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its zig

[LeetCode] 103. Binary Tree Zigzag Level Order Traversal Java

题目: Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example:Given binary tree [3,9,20,null,null,15,7], 3 / 9 20 / 15 7 retur

Leetcode-103(Java) Binary Tree Zigzag Level Order Traversal

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its zigz

Leetcode 103. Binary Tree Zigzag Level Order Traversal

1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 vector<vector<int>>

leetCode 103.Binary Tree Zigzag Level Order Traversal (二叉树Z字形水平序) 解题思路和方法

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its zig

【leetcode】Binary Tree Zigzag Level Order Traversal

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7return its z

leetcode No103. Binary Tree Zigzag Level Order Traversal

Question: Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree [3,9,20,null,null,15,7], 3 / 9 20 / 15

leetCode(23):Binary Tree Zigzag Level Order Traversal

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its zig