Exemple #1
0
    private static void FillMap(TreeNodeVO nodeVO, int key, int depth)
    {
        if (nodeVO == null)
        {
            return;
        }

        color[key].Add(new NodeVO(nodeVO.val, depth));

        FillMap(nodeVO.left, key - 1, depth + 1);
        FillMap(nodeVO.right, key + 1, depth + 1);
    }
Exemple #2
0
    private static void FindMinMax(TreeNodeVO nodeVO, int key)
    {
        if (nodeVO == null)
        {
            return;
        }

        min = Math.Min(min, key);
        max = Math.Max(max, key);

        FindMinMax(nodeVO.left, key - 1);
        FindMinMax(nodeVO.right, key + 1);
    }
Exemple #3
0
    public static IList <IList <int> > VerticalTraversal(TreeNodeVO root)
    {
        IList <IList <int> > result = new List <IList <int> > ();

        FindMinMax(root, 0);

        for (int i = min; i <= max; i++)
        {
            color[i] = new List <NodeVO> ();
        }

        FillMap(root, 0, 0);

        foreach (var value in color.Values)
        {
            value.Sort((x, y) => {
                if (x.depth == y.depth)
                {
                    return(x.val - y.val);
                }
                else
                {
                    return(x.depth - y.depth);
                }
            });

            List <int> temp = new List <int> ();

            foreach (var n in value)
            {
                temp.Add(n.val);
            }

            result.Add(temp);
        }

        return(result);
    }
Exemple #4
0
 public TreeNodeVO(int val = 0, TreeNodeVO left = null, TreeNodeVO right = null)
 {
     this.val   = val;
     this.left  = left;
     this.right = right;
 }